﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqStudents
{
    class LINQStudents
    {
        static void Main()
        {
            Student[] students = new Student[] { new Student("Dimitar","Dimitrov",20),
            new Student("Iliqn","Iliev",21),
            new Student("Kristina","Sybeva",22),
            new Student("Ivan","Atanasov",17),
            new Student("Pesho","Ivanov",26)};

            Console.WriteLine("lambda expression:");
            var sortedStudents = students.Where(s => s.FirstName.CompareTo(s.LastName) < 0);
            foreach (Student s in sortedStudents)
            {
                Console.WriteLine(s.FirstName + " " + s.LastName);
            }

            Console.WriteLine("\nUsing linq:");
            var sortedStudentsLinq = from s in students
                                     where s.FirstName.CompareTo(s.LastName) < 0
                                     select s;
            foreach (Student s in sortedStudentsLinq)
            {
                Console.WriteLine(s.FirstName + " " + s.LastName);
            }

            var studentsByAge = from s in students
                                where s.Age >= 18 && s.Age <= 24
                                select s;
            Console.WriteLine("\nStudents by age");
            foreach (Student s in studentsByAge)
            {
                Console.WriteLine(s.FirstName + " " + s.LastName);
            }

            var studentsByName = students.OrderByDescending(s => s.FirstName).ThenBy(s => s.LastName);
            Console.WriteLine("\nStudents by names");
            foreach (Student s in studentsByName)
            {
                Console.WriteLine(s.FirstName + " " + s.LastName);
            }

            Console.WriteLine("\nStudents by names LINQ");
            var studentsByNameLinq = from p in
                                         (from s in students
                                          orderby s.LastName descending
                                          select s)
                                     orderby p.FirstName descending
                                     select p;
            foreach (Student s in studentsByNameLinq)
            {
                Console.WriteLine(s.FirstName + " " + s.LastName);
            }
        }
    }
}
